#@layout()
#define script()
<script src="#(CPATH)/static/components/ckeditor/ckeditor.js"></script>
<script src="#(CPATH)/static/components/editable/js/bootstrap-editable.min.js"></script>
<script src="#(CPATH)/static/components/selectize/selectize.min.js"></script>
<script src="#(CPATH)/static/components/highlight/highlight.pack.js"></script>
<script src="#(CPATH)/static/components/simplemde/simplemde.min.js"></script>


<script>

    var $select = $('.article-tags').selectize({
        plugins: ['remove_button'],
        delimiter: ',',
        persist: false,
        addPrecedence: true,
        create: true,
        render: {
            option_create: function (data, escape) {
                return '<div class="create" style="padding-left: 5px"> 添加标签：<strong style="color: #DD1144">' + escape(data.input) + '</strong> &hellip; </div>';
            }
        }
    });

    var selectize = $select[0].selectize;

    $(".newTag").each(function () {
        $(this).on('click', function () {
            selectize.createItem($(this).text());
        })
    });


    $(".tflag").each(function () {
        $(this).on('click', function () {
            $("#flag").val($(this).text());
        })
    });


    $(".submitBtn").on("click", function () {
        $("#articleStatus").val($(this).attr("data-status"));
        doSubmit();
    });

    // 全局函数,在CKEditor ctrl + s 快捷键处用到,保留article的status
    window.doSubmit = function () {
        ajaxSubmit("#form", function (data) {
            $("#articleId").attr("value", data.id);
            toastr.success('文章保存成功。');
        });
    }

    $("#removeThumbnail").on("click", function () {
        $("#thumbnail").attr("src", "#(CPATH)/static/commons/img/nothumbnail.jpg");
        $("#articleThumbnail").val("");
    })

    var editMode = "#(editMode ?? 'html')";

    function doChangeEditor(id) {
        if (confirm('确定要切换编辑器吗？切换可能出现前台渲染内容缺失的问题。')) {
            var changeMode = editMode == "html" ? "markdown" : "html";
            if (id != '0') {
                ajaxGet('#(CPATH)/admin/article/doChangeEditMode?id=' + id + "&mode=" + changeMode);
            } else {
                ajaxGet('#(CPATH)/commons/changeEditor/' + changeMode);
            }
        }
    }

    initEditor('editor1', 467, editMode);

    doListenerCtrlsAndCommands(doSubmit);


</script>
#end
#define css()
<link href="#(CPATH)/static/components/editable/css/bootstrap-editable.css" rel="stylesheet">
<link href="#(CPATH)/static/components/selectize/css/selectize.css" rel="stylesheet">
<link href="#(CPATH)/static/components/selectize/css/selectize.jpress.css" rel="stylesheet">
<link href="#(CPATH)/static/components/highlight/styles/github.css" rel="stylesheet">
<link href="#(CPATH)/static/components/simplemde/simplemde.min.css" rel="stylesheet">
<link href="#(CPATH)/static/components/simplemde/simplemde.jpress.css" rel="stylesheet">
<style>
    .popover-content {
        padding: 9px 14px;
        margin: 0 10px;
    }

    #slug {
        cursor: pointer;
    }
</style>
#end

#define content()
<div class="content-wrapper">

    <section class="content-header">
        <h1>
            写文章
        </h1>
    </section>

    <section class="content">
        <form action="#(CPATH)/admin/article/doWriteSave" method="post" id="form">
            <input type="hidden" id="articleStatus" name="article.status" value='#(article.status ?? "normal")'>
            <input type="hidden" id="articleId" name="article.id" value="#(article.id ??)">
            <input type="hidden" name="article.user_id" value="#(article.user_id ?? USER.id)">
            <input type="hidden" id="editModeId" name="article.edit_mode" value="#(editMode ??)">
            <div class="row">
                <div class="col-lg-9">

                    <!-- /.box-header -->
                    <!-- form start -->
                    <div class="form-horizontal">
                        <div class="form-group">
                            <div class="col-sm-12">
                                <input type="input" class="form-control input-lg" name="article.title"
                                       placeholder="请输入标题"
                                       id="article-title"
                                       value="#(article.title ??)">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-12" style="padding-bottom: 2px">
                                网址：<span class="domainSpan">#option('web_domain')</span>#(CPATH)/article/<span id="slug"
                                                                                                               class="slugSpan"
                                                                                                               for-input="article-slug">#(article.slug ??)</span>#if(option("web_fake_static_enable"))#option('web_fake_static_suffix')#end
                                #if(article && article.isNormal())
                                （<a href="#(article.url)" target="_blank">访问</a>）
                                #end
                                <input type="hidden" id="article-slug" name="article.slug" value="#(article.slug ??)">
                                <a href="javascript:;" style="float: right;padding: 5px 0" onclick="doChangeEditor('#(article.id ?? 0)')">切换编辑器</a>
                            </div>
                            <div class="col-sm-12" style="z-index: 9999;">
                                <textarea id="editor1" name="article.content"
                                          style="height: 467px;width: 100%;background-color: white">#(article._getEditContent() ??)</textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-12">
                                <div class="box box-solid">
                                    <div class="box-header with-border">
                                        <h3 class="box-title">属性设置</h3>

                                        <div class="box-tools">
                                            <button type="button" class="btn btn-box-tool" data-widget="collapse">
                                                <i class="fa fa-minus"></i>
                                            </button>
                                        </div>
                                    </div>
                                    <!-- /.box-header -->
                                    <div class="box-body ">
                                        <div class="box-body ">

                                            #(fields.render())

                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>

                <div class="col-lg-3">
                    <div class="box box-solid">
                        <div class="box-body ">

                            <button type="button" data-status="normal" class="btn btn-primary btn-sm submitBtn">发布
                            </button>
                            <button type="button" data-status="draft" class="btn btn-default btn-sm submitBtn">草稿
                            </button>
                            <a href="#" data-status="trash" class="pull-right submitBtn"
                               style="padding-top: 5px;">移至垃圾箱</a>
                        </div>
                    </div>

                    <div class="box box-solid">
                        <div class="box-header with-border ">
                            <h3 class="box-title">模板样式</h3>
                        </div>
                        <div class="box-body ">

                            #if(styles && styles.size() > 0 )
                            <div class="form-group">
                                <select class="form-control" name="article.style">
                                    <option value="">默认样式</option>
                                    #for(style : styles)
                                    <option value="#(style)" #selectedIf(article && article.style==style)>
                                        #(style)
                                    </option>
                                    #end
                                </select>
                            </div>
                            #end

                            <div class="form-group">
                                <input type="text" class="form-control" name="article.flag"
                                       placeholder="展示标识" value="#(article.flag ??)" id="flag"/>

                                #if(flags && flags.size() > 0)
                                <p class="help-block">当前模板支持的展示标识有：</p>
                                <div class="tags">
                                    #for(flag : flags ??)
                                    <a href="javascript:;" class="tflag">#(flag)</a>
                                    #end
                                </div>
                                #end

                            </div>
                        </div>
                    </div>


                    <div class="box box-solid">
                        <div class="box-header with-border ">
                            <h3 class="box-title">分类</h3>
                        </div>
                        <div class="box-body ">
                            <div class="form-group">
                                #for(category : categories)
                                <div class="checkbox">
                                    <label>
                                        #for(i = 0; i < category.layerNumber; i++)
                                        &nbsp;&nbsp;&nbsp;&nbsp;
                                        #end

                                        #if(category.isCheck)
                                        <input type="checkbox" name="category" value="#(category.id)" checked/>
                                        #else
                                        <input type="checkbox" name="category" value="#(category.id)"/>
                                        #end


                                        #(category.title ??)
                                    </label>
                                </div>
                                #end
                            </div>
                        </div>
                    </div>

                    <div class="box box-solid">
                        <div class="box-header with-border ">
                            <h3 class="box-title">标签</h3>
                        </div>
                        <div class="box-body">
                            <select class="form-control article-tags" multiple="multiple" name="tag">
                                #for(tag : tags)
                                <option value="#(tag.title ??)" selected>#(tag.title ??)</option>
                                #end
                            </select>
                            <p class="help-block">多个标签请用 “回车键” 或英文逗号（,）隔开。</p>

                            <div class="tags">
                                #tags(count = 20)
                                #for(tag : tags)
                                <a href="javascript:;" class="newTag">#(tag.title)</a>
                                #end
                                #end
                            </div>
                        </div>
                    </div>
                    <div class="box box-solid">
                        <div class="box-header with-border ">
                            <h3 class="box-title">缩略图</h3>
                        </div>
                        <div class="box-body no-padding">
                            <img src="#(CPATH)#(article.thumbnail ?? '/static/commons/img/nothumbnail.jpg')"
                                 style="width: 100%;height: 200px"
                                 id="thumbnail">
                            <input type="hidden" value="#(article.thumbnail ??)" name="article.thumbnail"
                                   id="articleThumbnail">
                        </div>
                        <div class="box-footer">
                            <button type="button" class="jp-image-browser btn btn-default btn-sm "
                                    for-src="thumbnail"
                                    for-input="articleThumbnail">选择图片
                            </button>
                            <a href="javascript:;" style="padding-left: 15px" id="removeThumbnail">移除</a>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </section>

</div>
#end
